var DBFactory = require('../db/mysql_db');
var categoryDao = module.exports;

categoryDao.save = function(data, cb) {
    if(!data) return cb(new ERR(500));
    var arr = {};
    arr.name = data.name;
    arr.created_date = data.created_date;
    if(data.id) arr.id = data.id;
    var sql = 'replace into shop_category set ? ';

    DBFactory.poolQuery(sql, arr,
        function(){
            cb('no collection');
        },function (err,result) {
            if(err){
                cb(err);
                console.log('save Err: ' + err);
                return;
            }
            cb(null, '编辑或添加类别成功！');
        }
    );
}

categoryDao.delete = function(data, cb) {
    if(!data) return cb(new ERR(500));
    var sql = 'delete from shop_category where id = ? ';
    DBFactory.poolQuery(sql, [data.id],
        function(){
            cb('no collection');
        },function (err,result) {
            if(err){
                cb(err);
                console.log('delete Err: ' + err);
                return;
            }
            cb(null, '删除类别成功！');
        }
    );
}

categoryDao.list = function(data, cb) {
    if(!data) return cb(new ERR(500));
    var sql = 'select * from shop_category limit ?, ?';
    DBFactory.poolQuery(sql, [data.start, data.size],
        function(){
            cb('no collection');
        },function (err,result) {
            if(err){
                cb(err);
                console.log('list Err: ' + err);
                return;
            }
            cb(null, result);
        }
    );
}